package com.mid;

/**
 * Created by Lxk on 2020/1/28.
 */
public class Solution1302 {

    /**
     * 深度优先搜索解决方案
     */
    private Integer countMaxDep = -1;

    private Integer totalSum = 0;

    private void deepestLeavesTotal(TreeNode node, Integer depth) {
        if (node == null) {
            return;
        }
        if (depth > countMaxDep) {
            totalSum = node.val;
            countMaxDep = depth;
        } else if (depth.equals(countMaxDep)) {
            totalSum += node.val;
        }
        deepestLeavesTotal(node.left, depth + 1);
        deepestLeavesTotal(node.right, depth + 1);
    }

    public int deepestLeavesSum(TreeNode root) {
        deepestLeavesTotal(root, 0);
        return totalSum;
    }

}
